package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.StudentSchool;
import com.yuyou.fn.educational.mapper.StudentSchoolMapper;
import com.yuyou.fn.educational.service.IStudentSchoolService;
import com.yuyou.fn.educational.vo.StudentSchoolQueryVo;
import com.yuyou.fn.educational.vo.StudentSchoolSaveOrUpdateVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * StudentSchool 表数据服务层接口实现类
 *
 */
@Service
public class StudentSchoolServiceImpl extends SuperServiceImpl<StudentSchoolMapper, StudentSchool,Long> implements IStudentSchoolService {

    @Override
    public Page<StudentSchool> findStudentSchoolPageByQuery(StudentSchoolQueryVo studentSchoolQueryVo, int pageNo, int pageSize) {
        Example<StudentSchool> example = StudentSchool.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,studentSchoolQueryVo);
        return this.findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public Page<StudentSchool> findStudentSchoolForPage(String city,String schoolName , int pageNo, int pageSize) {
        Example<StudentSchool> example = StudentSchool.createExample();
        Example.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotEmpty(city)){
            criteria.andEqualTo("city",city );
        }

        if(StringUtils.isNotEmpty(schoolName)){
            criteria.andLike("schoolName" , "%"+schoolName+"%") ;
        }
        example.orderBy("province").orderBy("city").orderBy("district");
        return findPageByExample(example,pageNo,pageSize );
    }

    @Override
    public void saveOrUpdateStudentSchool(StudentSchoolSaveOrUpdateVo studentSchoolSaveOrUpdateVo) {

        StudentSchool studentSchool = new StudentSchool();

        BeanUtils.copyProperties(studentSchool, studentSchoolSaveOrUpdateVo);

        checkSchool(studentSchool);

        if (studentSchool.getStudentSchoolId() == null) {
            studentSchool.setCreateTime(TimeUtils.now());
            studentSchool.setUpdateTime(TimeUtils.now());
            insertSelective(studentSchool);
        } else {
            studentSchool.setUpdateTime(TimeUtils.now());
            updateSelectiveById(studentSchool);
        }

    }

    private void checkSchool(StudentSchool studentSchool) {

        Example<StudentSchool> example = StudentSchool.createExample();
        example.createCriteria().andEqualTo("schoolName",studentSchool.getSchoolName() )
                .andEqualTo("province",studentSchool.getProvince() )
                .andEqualTo("district",studentSchool.getDistrict() )
                .andEqualTo("city",studentSchool.getCity());

        StudentSchool one = findOneByExample(example);

        if(one != null && ( studentSchool.getStudentSchoolId() == null || !studentSchool.getStudentSchoolId().equals(one.getStudentSchoolId()) ) ){
            throw new BusException("此生源学校已经录入了");
        }
    }

    @Override
    public List<StudentSchool> searchStudentSchool(String query) {
        Example<StudentSchool> example = StudentSchool.createExample();
        example.limit(20);
        example.createCriteria().andLike("schoolName","%"+query+"%");
        return findListByExample(example);
    }

    @Override
    public StudentSchool findSchoolBySchoolName(String schoolName){
        Example<StudentSchool> example = StudentSchool.createExample();
        example.createCriteria().andEqualTo("schoolName", schoolName.trim());
        return findOneByExample(example);
    }


    @Override
    public List<StudentSchool> searchStudentSchool(String province, String city, String district, String query) {
        Validate.notNull(province,"Required province");
        Validate.notNull(city,"Required city");
        Validate.notNull(district,"Required district");

        Example<StudentSchool> example = StudentSchool.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("province" , province).andEqualTo("city" ,city).andEqualTo("district" ,district );
        if(StringUtils.isNotEmpty(query)){
            criteria.andLike("schoolName","%"+query+"%");
        }
        return findListByExample(example);
    }
}